import mouse
import os, sys
import time
from multiprocessing.managers import BaseManager

class MyManager(BaseManager):
    pass

# mouse.initialize()

Port = 6018

def isPortInUse(port):
    import socket

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        try:
            s.bind(("localhost", port))
            return False
        except:
            return True

def test_pyautogui(pyautogui):
    pyautogui.click(250, 80)  # 在坐标 (100, 200) 处点击鼠标左键


# if __name__ in os.path.splitext(os.path.basename(__file__))[0]:
if __name__ =="__main__":
    # import pyautogui
    
    # test_pyautogui(pyautogui)
    # sys.exit(0)
    
    MyManager.register("pyautogui")
    m = MyManager(address=("", Port), authkey=b"abracadabra")

    ts = time.time()
    while not isPortInUse(Port):
        time.sleep(0.2)
        if time.time() - ts > 10:
            raise Exception("loading pyautogui timeout")

    while True:
        try:
            m.connect()
        except Exception as e:
            if time.time() - ts > 10:
                raise Exception("loading pyautogui timeout")
            time.sleep(0.1)
            continue
        break

    pyautogui = m.pyautogui()
    ###############################################################
    
    test_pyautogui(pyautogui)
    print('')
    
    # print("\033[36;1mAlgorithm version:", Algorithm.version(), "\033[0m")
